package com.phone;

import com.phone.handle.UserHandle;
import org.hibernate.Session;

import java.util.Random;

/**
 * Created by tisen on 2017/7/6.
 */
public class InviteCode {
    private static final int MAX_CODE = 16 * 16 * 16 * 16 * 16 * 16;
    private static Random random = new Random();

    public static String getInviteCode(){
        Session session = DBHelper.getSession();
        session.beginTransaction();
        UserHandle handle = new UserHandle(session);

        String code = Integer.toHexString(random.nextInt(MAX_CODE));
        boolean flag = true;
        while (flag){
            try {
                if(!handle.isCode(code)){
                    flag = false;
                }else {
                    code = Integer.toHexString(random.nextInt(MAX_CODE));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return code;
    }
}
